<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class Session1Controller extends Controller
{
    /**
     * 版块的添加
     */
    public function getAdd(Request $request)
    { 
        //读取所属分区
        $res = cateController::getCateInfo();
        // dd($res);
        //分配
    	return view('admin.session.add',['cate'=>$res]);
    }
    /**
     * 版块的添加
     */
    public function postInsert(Request $request)
    {
    	//进行表单验证
    	$this->validate($request,[
    	// 		//用户名=>必填|格式|名字唯一
    			'sname' => 'required|unique:session,sname',
    		    
    		],[
    			'sname.required' => '版块名不能为空',
    		]);
    	//获取数据
    	$data = $request->except(['_token']);
        // dd($data);
    	//处理logo
    	if($request->hasFile('logo')){
    		$fileName = date('Ymd',time()).rand(100000,999999);
    		$suffix = $request->file('logo')->getClientOriginalExtension();//获取文件后缀名
    		$fileName = $fileName.'.'.$suffix;
    		$request->file('logo')->move('./Uploads/',$fileName);
    		$data['logo'] = '/Uploads/'.$fileName;
    	}

    	//插入数据
    	$res = DB::table('session')->insert($data);
        // dd($res);
        

    	// 检测
    	if($res){
    		return redirect('/session/index')->with('info','添加成功 ');
    	}else{
    		return back()->with('error','添加失败');
    	}
    }

    /**
     * 列表显示功能
     */
    public function getIndex(Request $request)
    {
        $res = DB::table('session')->select(db::raw('*'))->paginate($request->input('num',1));
        // dd($res);
    	return view('admin.session.index',['session1'=>$res,'request'=>$request]);
        // dd($session);
    }

    /**
     * 版块的修改页面
     */
    public function getEdit(Request $request)
    {
        
    	//读取当前id的所属版块信息
    	$id = $request->input('id');
    	//读取数据库
    	$sname = DB::table('session')->where('id',$id)->first();
        // dd($sname);
    	//解析模板
    	return  view('admin.session.edit',['sname'=>$sname]);
    }

    /**
     * 版块的修改操作
     */
    public function postUpdate(Request $request)
    {
         $id = $request->input('id');
    	//表单验证
    	$this->validate($request,[
    			//用户名=>必填|格式|名字唯一
    			'sname' => 'required|unique:session,sname',
    		],[
    			'sname.required' => '用户名不能为空',
    		]);
    	//图片上传操作
    	$data = $request->except(['_token']);
    	//处理上传的文件
    	$path = $this->upload($request);
    	if(!empty($path)){
    		$data['logo'] = $path;
    	}
    	//更新
    	$res = DB::table('session')->where('id',$request->input('id'))->update($data);
    	// dd($res);
    	if($res){
    		return redirect('/session/index')->with('info','修改成功');
    	}else{
    		return back()->with('error','修改失败');
    	}
    }

    /**
     * 版块删除操作
     */
    public function getDelete(Request $request)
    {
    	$id = $request->input('id');
    	//执行删除
    	$res = DB::table('session')->where('id',$id)-> delete();
    	if($res){
    		return back()->with('info','删除成功');
    	}else{
    		return back()->with('error','删除失败');
    	}
    }

    /**
     * 封装上传操作代码
     */
    public function upload($request)
    {
    	if($request->hasFile('logo')){
    		$fileName = date('Ymd',time()).rand(100000,999999);
    		$suffix = $request->file('logo')->getClientOriginalExtension();//获取文件后缀名
    		$fileName = $fileName.'.'.$suffix;
    		$request->file('logo')->move('./Uploads/',$fileName);
    		$data['logo'] = '/Uploads/'.$fileName;
    		return $data['logo'];
    	}
    }


   


}


